package org.cid54.jshopper.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.cid54.jshopper.dao.ShoppingListDao;
import org.cid54.jshopper.dao.UserGroupDao;
import org.cid54.jshopper.model.ShoppingList;
import org.cid54.jshopper.model.UserGroup;
import org.cid54.jshopper.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author fgo
 */
@Service
@Transactional
public class HomeServiceImpl implements HomeService {

    @Autowired
    private ShoppingListDao shoppingListDao;
    @Autowired
    private UserGroupDao userGroupDao;

    @Override
    public List<ShoppingList> getShoppingListByUserGroups(List<UserGroup> userGroups) {

        if (!userGroups.isEmpty()) {
            List<String> userGroupNames = new ArrayList<String>();

            for (UserGroup group : userGroups) {
                userGroupNames.add(group.getName());
            }

            return this.getShoppingListByUserGroupNames(userGroupNames);

        } else {
            return new ArrayList<ShoppingList>();
        }

    }

    @Override
    public List<ShoppingList> getShoppingListByUserGroupNames(List<String> userGroupNames) {

        if (!userGroupNames.isEmpty()) {
            List<UserGroup> userGroups = new ArrayList<UserGroup>();

            for (String name : userGroupNames) {
                userGroups.add(this.userGroupDao.findByName(name));
            }

            return this.shoppingListDao.findByUserGroups(userGroups);
            
        } else {
            return new ArrayList<ShoppingList>();
        }

    }
}
